package com.example.administrator.myapplication.view.other;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.base.BaseActivity;
import com.example.administrator.myapplication.mode.UserInfo;
import com.example.administrator.myapplication.utils.AnimUtils;
import com.example.administrator.myapplication.utils.Global;
import com.example.administrator.myapplication.view.HomeActivity;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 说明：
 * 作者： 阳2012; 创建于： 2017/5/6 21:25
 */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.phone)
    TextInputLayout phone;
    @BindView(R.id.password)
    TextInputLayout password;
    @BindView(R.id.commitLogin)
    TextView commitLogin;


    MyBroadcast myB = new MyBroadcast();
    IntentFilter intentFilter = new IntentFilter();


    boolean mIsLogin;
    public String[] titles = new String[]{"登录", "注册 "};

    @Override
    public int getContentView() {
        return R.layout.activity_login;
    }


    public static void startUI(Context context, boolean isLogin) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.putExtra("boundWx", true);
        intent.putExtra("isLogin", isLogin);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        intentFilter.addAction(Global.LOGIN_SUCCEED);
        intentFilter.addAction(Global.REGIST_SUCCEED);
        registerReceiver(myB, intentFilter);

    }

    @Override
    public void parseIntent(Intent intent) {
        mIsLogin = intent.getBooleanExtra("isLogin", false);

        if (mIsLogin) {
            toolbar.setTitle(titles[0]);
        } else {
            toolbar.setTitle(titles[1]);
            commitLogin.setText(titles[1]);
        }
    }

    /*
     * 初始化view
     */
    public void initView() {


        toolbar.setNavigationIcon(R.drawable.material_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @OnClick({R.id.verification, R.id.commitLogin, R.id.wechat})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.verification:
                showWarningMessage("功能没有添加");
                break;
            case R.id.commitLogin:
                if (phone.getEditText().getText().toString().length() != 11) {
                    AnimUtils.shakeLeft(phone, 0.85f, 6f);
                    showWarningMessage("请正确输入手机号");
                    return;
                }

                if (password.getEditText().getText().toString().length() == 0) {
                    AnimUtils.shakeLeft(password, 0.85f, 6f);
                    showWarningMessage("请输入密码");
                    return;
                }


                if (mIsLogin) {
                    String account = phone.getEditText().getText().toString().trim();
                    String pa = password.getEditText().getText().toString().trim();
                    UserInfo.checkLogin(new UserInfo(account, pa), this);
                } else {
                    String account = phone.getEditText().getText().toString().trim();
                    String pa = password.getEditText().getText().toString().trim();
                    UserInfo.save(new UserInfo(account, pa), this);
                }


                break;
            case R.id.wechat:
                showWarningMessage("功能没有添加");
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myB);
    }

    ProgressDialog progressDialog = null;

    /**
     * 说明：收藏登录 与注册广播
     * 作者： 阳2012; 创建于： 2017/5/6 22:51
     */
    class MyBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Global.LOGIN_SUCCEED)) {

                progressDialog = ProgressDialog.show(LoginActivity.this, "请稍等...", "正在登录...", true);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.dismiss();
                        HomeActivity.startUI(LoginActivity.this);
                        finish();
                    }
                }, 3000);

            } else if (intent.getAction().equals(Global.REGIST_SUCCEED)) {
                showInfo("注册成功");
                finish();
            }

        }
    }

}
